package com.example.cart_system.controller;

import com.example.cart_system.pojo.Cart;
import com.example.cart_system.pojo.GoodsInfo;
import com.example.cart_system.pojo.Result;
import com.example.cart_system.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

@RestController
public class CartController {
    @Autowired
    private CartService cartService;

    @PostMapping("/addcart")
    public Result<Boolean> addCart(@RequestBody Cart cart) {
        boolean f =cartService.addCart(cart);
        return f ? new Result<Boolean>().success("添加购物车") : new Result<Boolean>().success("数量加一");
    }

    @PostMapping("/cartlist")
    public Result<List<GoodsInfo>> getCartList(@RequestBody Cart cart) {
        List<GoodsInfo> cartlist = cartService.getCartList(cart.getUsrid());
        return new Result<List<GoodsInfo>>().success(cartlist);
    }

    @PostMapping("/removecart")
    public Result<Boolean> removeCart(@RequestBody Cart cart) {
        cartService.removeCart(cart.getGid(), cart.getUsrid());
        return new Result<Boolean>().success(true);
    }

    @PostMapping("/reducecart")
    public Result<Boolean> reduceCart(@RequestBody Cart cart) {
        cartService.reduceCnn(cart);
        return new Result<Boolean>().success(true);
    }

    //清空购物车
    @PostMapping("/clearcart")
    public Result<Boolean> clearCart(@RequestBody Cart cart) {
        cartService.clearCart(cart.getUsrid());
        return new Result<Boolean>().success(true);
    }
}
